package com.yanqu.road.server.agent.inter;

import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.entity.enums.eAgentErrorCode;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.cross3.server.CrossChannelServerProto;
import com.yanqu.road.pb.worldmessage.WorldMessageProto;
import com.yanqu.road.server.agent.AgentInterfaceMgr;
import com.yanqu.road.server.channelserver.logic.ChannelMessageHelper;
import com.yanqu.road.server.channelserver.manager.activity.washe.ChannelWasheMgr;
import com.yanqu.road.server.protocol.Cross3Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

public class ChannelFunction {

    public String clearActivityChat(JSONObject param) {
        JSONObject ret = AgentInterfaceMgr.getDefaultRet();
        String content = param.getString("content");
        try {
            int activityId = Integer.valueOf(content);

            WorldMessageProto.SyncClearChatMsg.Builder builder = WorldMessageProto.SyncClearChatMsg.newBuilder();
            builder.setActivityId(activityId);
            YanQuMessage message = YanQuMessageUtils.buildMessage(Cross3Protocol.C3_CLEAR_CHANNEL_ACTIVITY_CHAT, builder);
            ChannelMessageHelper.sendAllChannel(message);

            AgentInterfaceMgr.setCommonRet(ret, eAgentErrorCode.Success.getValue());
        } catch (Exception ex) {
            ex.printStackTrace();
            AgentInterfaceMgr.setCommonRet(ret, eAgentErrorCode.ArgumentError.getValue());
        }
        return ret.toString();
    }

    public String refreshActivitySwitch(JSONObject param) {
        JSONObject ret = AgentInterfaceMgr.getDefaultRet();
        String content = param.getString("content");
        try {
            int activityId = Integer.valueOf(content);

            CrossChannelServerProto.SyncRefreshChannelActivitySwitchRespMsg.Builder builder = CrossChannelServerProto.SyncRefreshChannelActivitySwitchRespMsg.newBuilder();
            builder.setActivityId(activityId);
            YanQuMessage message = YanQuMessageUtils.buildMessage(Cross3Protocol.C3_SYNC_REFRESH_CHANNEL_ACTIVITY_SWITCH, builder);
            ChannelMessageHelper.sendAllChannel(message);

            AgentInterfaceMgr.setCommonRet(ret, eAgentErrorCode.Success.getValue());
        } catch (Exception ex) {
            ex.printStackTrace();
            AgentInterfaceMgr.setCommonRet(ret, eAgentErrorCode.ArgumentError.getValue());
        }
        return ret.toString();
    }

    public String refreshWasheActivityConfig(JSONObject param) {
        JSONObject ret = AgentInterfaceMgr.getDefaultRet();
        try {
            ChannelWasheMgr.agentReloadConfig();
            AgentInterfaceMgr.setCommonRet(ret, eAgentErrorCode.Success.getValue());
        } catch (Exception ex) {
            ex.printStackTrace();
            AgentInterfaceMgr.setCommonRet(ret, eAgentErrorCode.ArgumentError.getValue());
        }
        return ret.toString();
    }

}
